package com.baiyou;

import java.awt.AWTException;
import java.awt.Frame;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * 功能：基本的窗体
 * @author Adam
 *
 */
public class BaseForm extends JFrame{
	//最小化到托盘
	private TrayIcon trayIcon;
	
	// 界面所需的实例变量
	private JLabel inputLabel = new JLabel("接收实时动态Email:");
	private JTextField textField = new JTextField("85614628@qq.com");
	private JButton startButton = new JButton("开始抓取");
	private JButton stopButton = new JButton("中断抓取");
	private JButton exitButton = new JButton("退出");
	private JTextArea displayArea = new JTextArea();
	
	// 构造函数，完成窗口的初始化
	public BaseForm() {
		//ICON:注意，打成jar包之后访问资源必须在src目录下，并且只能使用以下这种方式访问。
		java.net.URL imgURL = this.getClass().getResource("/img/64.png");//64px

		Image imageIcon = Toolkit.getDefaultToolkit().getImage(imgURL);
		//Toolkit.getDefaultToolkit().beep();//嘟嘟响
        setIconImage(imageIcon);

		setTitle("批量MD5加密：850148469@qq.com");
		setLayout(null);
		//setBounds
		//x1:x控件距离左边框距离
		//x2:y控件距离上边框距离
		//x3:width
		//x4:height
		inputLabel.setBounds(50, 10, 200, 30);
		textField.setBounds(50, 50, 300, 30);
		startButton.setBounds(50, 100, 90, 30);
		stopButton.setBounds(150, 100, 90, 30);
		exitButton.setBounds(250, 100, 90, 30);
		displayArea.setBounds(50, 150, 300, 50);

		// add the graphics components
		add(inputLabel);
		add(textField);
		add(startButton);
		add(stopButton);
		add(exitButton);
		add(displayArea);

		// add the ActionListener to the Button
		startButton.addActionListener(new ButtonAction());
		stopButton.addActionListener(new ButtonAction());
		exitButton.addActionListener(new ButtonAction());

		//默认窗体显示位置
		//窗体本身大小
		int width = 410;
		int height = 280;
		setSize(width, height);
		//窗体位置
		int w = (Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2;
		int h = (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2;
		setLocation(w, h);
		//设置窗口的大小是否可被改变
		setResizable(false);
		//设置用户在此窗体上发起 "close" 时默认执行的操作
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		//Icon和系统托盘
		java.net.URL imgURL2 = BaseForm.class.getResource("/resource/img/16.png");//16px
		Image imageIcon2 = Toolkit.getDefaultToolkit().getImage(imgURL2);
		this.initTrayIcon(imageIcon2);
	}
	
	
	/**
	 * 功能：初始化系统托盘
	 */
	private void initTrayIcon(Image imageIcon) {
		this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			//捕获窗口关闭事件
			public void windowClosing(WindowEvent e) {
				if (SystemTray.isSupported()) {
					setVisible(false);
					minimizeToTray();
				}else {
					System.exit(0);
				}
			}

			//捕获窗口最小化事件
			public void windowIconified(WindowEvent e) {
				if (SystemTray.isSupported()) {
					setVisible(false);
					minimizeToTray();
				}else {
					System.exit(0);
				}
			}
		});
		
		//系统托盘事件
		PopupMenu popup = new PopupMenu();
		//托盘右键显示事件
		MenuItem showItem = new MenuItem("Show");
		ActionListener showListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(true);
				setExtendedState(Frame.NORMAL);
				SystemTray.getSystemTray().remove(trayIcon);
			}
		};
		showItem.addActionListener(showListener);
		popup.add(showItem);
		//托盘邮件退出事件
		MenuItem exitItem = new MenuItem("Exit");
		ActionListener exitListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		};
		exitItem.addActionListener(exitListener);
		popup.add(exitItem);
		//托盘提示信息：根据image、提示、菜单创建TrayIcon
		this.trayIcon = new TrayIcon(imageIcon, "实时价格：", popup);
		//给TrayIcon添加事件监听器
		//单击托盘显示
		this.trayIcon.addMouseListener(new MouseAdapter() {
			@Override
		    public void mouseClicked(MouseEvent e) {
				if (e.getButton() == 1){// 单击托盘窗口再现  
					setVisible(true);
		    		setExtendedState(Frame.NORMAL);
		    		SystemTray.getSystemTray().remove(trayIcon);
				}else{//右键
					//System.exit(0);
				}
		    }
		});
		//双击托盘显示
		this.trayIcon.addActionListener(showListener);
	}
	
	/**
	 * 功能：添加系统托盘
	 */
	public void minimizeToTray() {
		SystemTray tray = SystemTray.getSystemTray();
		try {
			tray.add(this.trayIcon);
		} catch (AWTException ex) {
			ex.printStackTrace();
		}
	}
	
//	CrawlerThread ct = null;
	/**
	 * 功能：按钮事件
	 * @author Adam
	 *
	 */
	private class ButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == startButton) {
				if(!textField.getText().equals("")){
					displayArea.setText("开启抓取！");
					boolean b = false;//弹窗
//					ct = new CrawlerThread(b,textField.getText());
//					Thread startCrawl = new Thread(ct);
//					startCrawl.start();
					if(b){
						setVisible(false);
					}
				}else{
					displayArea.setText("请填写Email地址。");
//					InvokeSoundFile Isf = new InvokeSoundFile("resource/soundFile/no1.mp3");
//					Thread superGod= new Thread(Isf);
//					superGod.start();
				}
			}

			if (e.getSource() == stopButton) {
				//结束线程
//				ct.stopRequest();
				displayArea.setText("结束抓取！");
//				InvokeSoundFile Isf = new InvokeSoundFile("resource/soundFile/no3.mp3");
//				Thread stopCrawl= new Thread(Isf);
//				stopCrawl.start();
			}

			if (e.getSource() == exitButton) {
				System.exit(0);
			}
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BaseForm bm = new BaseForm();
	}

}
